#ifndef __thread__
#define __thread__
#include <pthread.h>
class Thread
{
private:
    pthread_t thread;
    pthread_attr_t attr;
    int stopped;
    Thread(const Thread& copy);         // copy constructor denied
    static void *thread_func(void *d)
    {
        if (!(((Thread *)d)->is_stopped()))
            ((Thread *)d)->run();
        else
        {
            pthread_exit(NULL);
            return NULL;
        }
        return NULL;
    }

public:
    Thread()             {
        stopped = 1;
    }
    virtual ~Thread()
    {
        if (!stopped)
            this->stop();
    }

    virtual void run() = 0;

    int start()
    {
        stopped = 0;
        int t;
        //pthread_attr_init(&attr);
       // pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
        t = pthread_create(&thread, NULL,
                           Thread::thread_func, (void*)this);
        return t;
    }
    int wait ()
    {
        //pthread_detach(thread);
        //pthread_join(thread, NULL);
        return 0;
    }
    int stop()
    {
        stopped = 1;
        return pthread_join(thread, NULL);
        //pthread_cancel(thread);

    }
    inline bool is_stopped() {return stopped;}
};
#endif
